package gonz.common;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class PriceAndArticleParser {
    private static final Logger log = Logger.getLogger(PriceAndArticleParser.class);
    public static final String PRICE_SOLIDS_ID = "Solid";
    public static final String PRICE_PRINTS_ID = "Print";
    public static final String PRICE_SPECIAL_ID = "Special";
    public static final String PRICE_ORIG_ID = "Orig";
    public static final String PRICE_SALE_ID = "Sale";
    public static final String PRICE_CLEARANCE_ID = "Clearance";
    private static final String ARTICLE_ID = "#";
    private static final String CURRENCY_SYMBOL = "$";
    public static final String UNKNOWN_SYMBOL = "";

    private static final String[] PRICE_END_DELIMETERS = new String[]{" ", "\n"};

    public static String parsePrice(String pricesAndArticle, String priceId) {
        int priceInd = pricesAndArticle.indexOf(priceId);
        if (priceInd == -1) {
            return UNKNOWN_SYMBOL;
        }
        int priceIndStart = pricesAndArticle.indexOf(CURRENCY_SYMBOL, priceInd);
        if (priceIndStart == -1) {
            log.error("Can't parse " + priceId + " price. Can't find \"" + CURRENCY_SYMBOL + "\" in :" + pricesAndArticle);
            return UNKNOWN_SYMBOL;
        }
        int priceIndEnd = Integer.MAX_VALUE;
        for (int i = 0; i < PRICE_END_DELIMETERS.length; i++) {
            int curPriceIndEnd = pricesAndArticle.indexOf(PRICE_END_DELIMETERS[i], priceIndStart);
            if (curPriceIndEnd != -1 && curPriceIndEnd < priceIndEnd)
                priceIndEnd = curPriceIndEnd;
        }

        if (priceIndEnd == -1) {
            log.error("Can't parse " + priceId + " price. Can't find price end in :" + pricesAndArticle);
            return UNKNOWN_SYMBOL;
        }
        return pricesAndArticle.substring(priceIndStart, priceIndEnd);
    }

    /**
     * Parse $123
     *
     * @param pricesAndArticle
     * @return
     */
    public static String parsePrice(String pricesAndArticle) {
        String[] parts = pricesAndArticle.split("\n");
        if (parts.length == 0) {
            log.error("Can't parse Price price. Can't split: " + pricesAndArticle);
            return UNKNOWN_SYMBOL;
        }
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].startsWith(CURRENCY_SYMBOL)) {
                return parts[i];
            }
        }

        return UNKNOWN_SYMBOL;
    }
    public static String parseAllPrices (String pricesAndArticle){
        String[] parts = pricesAndArticle.split("\n");
        String allPrices ="";
        if (parts.length == 0) {
            log.error("Can't parse Price price. Can't split: " + pricesAndArticle);
            return UNKNOWN_SYMBOL;
        }
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].startsWith(CURRENCY_SYMBOL)) {
                continue;
            }
            else
                allPrices+=parts[i];
        }
        if (StringUtils.isBlank(allPrices))
               return UNKNOWN_SYMBOL;
        else
            return allPrices;
    }
    /**
     * parse #123
     *
     * @param pricesAndArticle
     * @return
     */
    public static String parseArticle(String pricesAndArticle) {
        String[] parts = pricesAndArticle.split("\n");
        if (parts.length == 0) {
            log.error("Can't parse Article. Can't split: " + pricesAndArticle);
            return UNKNOWN_SYMBOL;
        }
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].startsWith(ARTICLE_ID)) {
                return parts[i];
            }
        }
        log.error("Can't parse Article price. " + pricesAndArticle);
        return UNKNOWN_SYMBOL;
    }
     public static boolean isArticleExist(String pricesAndArticle) {
         String[] parts = pricesAndArticle.split("\n");
         if (parts.length == 0) {
             return false;
         }
         for (int i = 0; i < parts.length; i++) {
             if (parts[i].startsWith(ARTICLE_ID)) {
                 return true;
             }
         }
         return false;
     }
}
